﻿@using MvcContrib.UI.Grid
@using XRM.Security.DataAccess.Helpers
@using XRM.Security.WebUI.MvcHelpers
@using XRM.Security.DataAccess.Enums
@using XRM.Security.WebUI.Helpers
@using XRM.Security.WebUI.Security
@model XRM.Security.WebUI.Models.UserRegister.UserRegisterSkziEditModel
@{
    Layout = null;
}
@functions {

    readonly string[] userRoles = UserPrincipal.Current.Roles;
    private bool IsUserInRole(IEnumerable<string> roles)
    {
        return roles.Intersect(userRoles).Any();
    }
}
<div class="filtered-table">
    @if (Model.SkziList.Any())
    {
        <div class="checkbox">
            <label for="@Html.IdFor( m => m.ShowDelete)">
                @Html.CheckBoxFor(x => x.ShowDelete)
                Отображать  удаленные
            </label>
        </div>
            Html.Grid(Model.SkziList).Columns(column =>
            {
                column.Custom(@<text>
                                   @if (Permission.AllowSkziKindView() && !item.IsDeletedSkzi)
                                   {
                                       @Html.ActionLink( item.SkziKindName, "Edit", "SkziKind", new { id = item.SkziKindId, userId = Model.UserRegisterId }, null )
                                   }
                                   else
                                   {
                                        @item.SkziKindName
                                   }
                               </text>).Named( "Вид СКЗИ" );
                column.For(item => item.SkziCopyNumber).Named("Экземпляр СКЗИ");
                column.For(item => item.SetupSystemUser.FullName()).Named("ФИО установившего");
                column.For( item => item.PCSerialNumber ).Named( "Серийный номер ПК" );
                column.For(item => item.SetupDate.ToString("dd.MM.yyyy")).Named("Дата установки");

                column.Custom(@<text>
                                    @if (item.DeleteDate.HasValue)
                                    {
                                        <text>@item.DeleteDate.Value.ToString("dd.MM.yyyy")</text>
                                        <i class="fa fa-comment" rel="tooltip" title="@item.DeleteReason" data-placement="top"></i>
                                    }
                              </text>).Named("Дата удаления");
                column.Custom(@<text>
                                @if ((!item.DeleteDate.HasValue) && (IsUserInRole(new[] { UserRoles.Administrator.ToString(), UserRoles.UsersReestrSkziEditor.ToString() })))
                                {
                                    @Html.DialogFormButton(new DialogButtonOptions()
                                    {
                                        Text = "Удалить СКЗИ",
                                        ContentUrl = Url.Action("DeleteUserSkziDialog", "UserRegister", new { userSkziId = item.Id }),
                                        Width = "800",
                                        Id = "dlgDeleteUserSkziDialog" + item.Id,
                                        ButtonId = "btnDeleteUserSkzi" + item.Id,
                                        OnSuccess = "RefreshPage",
                                        Image = "fa-trash-o"
                                    })
                                }
                              </text>)
                      .Named( "<i class='fa fa-trash-o fa-action'></i>" ).Attributes( @class => "single-action" );

            }).Attributes(@class => "table table-striped table-bordered", id => "table_userSkzi_id")
            .RowAttributes( item => new MvcContrib.Hash( @class => item.Item.DeleteDate.HasValue ? "inactive hide" : "" ) )
            .Render();
    }
    else
    {
        <p>Нет установленных СКЗИ.</p>
    }
</div>

<script type="text/javascript">
    $("#ShowDelete").change(function () {
        if ($(this).is(":checked")) {
            $("#table_userSkzi_id .inactive").removeClass("hide");
        }
        else {
            $("#table_userSkzi_id .inactive").addClass("hide");
        }
    });
    $(document).ready(function () {
        @if (Model.SkziList.Any())
        {
                <text>
                $('#table_userSkzi_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: false,
                    info: false
                });
                </text>
        }

        $('[rel=tooltip]').tooltip();
        BindDialogButtons();
        $("#ShowDelete").change();
    });
</script>
